<?php
namespace app\super\controller;
use think\Db;
use think\Controller;
use  think\Request;
use think\Config;

class User extends Base
{
    
	public function Index(){
		
		
		
		
		
		 return view();
	}
	
	
	
	function Add(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["username"] =$request->post('username');
			
				$data["state"] =$request->post('state');
				$data["jiaoseid"] =$request->post('jiaoseid');
				$data["mid"] =$request->post('mid');
				$data["createtime"] =  date("Y-m-d h:i:sa");
				
			
				
			if($data["id"]){
				
			
				
				if(db('manage')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				$data["password"] =  md5($request->post('password'));
			
			  if(db('manage')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'添加失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('manage')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
			
			
		
		
	
		
			
		}
		
		
	
	
		return view();
	}
	 public function GetUser(){
		$where=" 1=1 ";
		if($_GET["super"]=="1"){
			$where=$where." and issuper=1";
		}

		$count =db('manage')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('manage')->where($where)->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			if($value["jiaoseid"]){
				$jiaoseinfo = db('sys_jiaose')->where("id=".$value["jiaoseid"])->find();
				if($jiaoseinfo){
					$value["jiaosename"]= $jiaoseinfo["jiaosename"];
				}
			}
			
			
			
		}
		
		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	
	function Del(){
		$request = Request::instance();
		$id =$request->post('id');
		
		
		 
			$where = 'id='.$id;
		  
		   $list=db('manage')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	public function alluser(){
		
		return view();
	}
	public function getalluser(){
		$where=" 1=1 ";
		$first = $_POST["size"] * ($_POST["page"] -1);
		if($_POST["stime"]&&$_POST["etime"]){
			$stime = $_POST["stime"]." 00:00:00";
			$etime = $_POST["etime"]." 23:59:59";
			$where=$where." and users.createtime between '".$stime."' and '".$etime."'";
		}
		if($_POST["typeid"]){
			$where=$where." and users.type=".$_POST["typeid"];
		}
		if($_POST["iskefu"]){
			$where=$where." and users.iskefu=".$_POST["iskefu"];
		}
		if($_POST["mechanismname"]){
			$where=$where." and mechanism.name like '%".$_POST["mechanismname"]."%' ";
			$count =db('users')->join("mechanism","mechanism.id = users.mid")->where($where)->count();
			$list = db('users')->join("mechanism","mechanism.id = users.mid")->where($where)->field("users.*,mechanism.name as mname")->order("id desc")->limit($first,$_POST["size"])->select();
		}else{
			$count =db('users')->where($where)->count();
			$list = db('users')->where($where)->field("users.*")->order("id desc")->limit($first,$_POST["size"])->select();
			foreach ($list as $key => &$value) {
				$mechanism = db("mechanism")->where("id=".$value["mid"])->find();
				if($mechanism){
					$value["mname"]=$mechanism["name"];
				}
			
			}
		}
		
		
		
		$obj["count"]=(int)($count/$_POST["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);

	}
	public function userdel(){
		$request = Request::instance();
		$id =$request->post('id');

		$user = db("users")->where("id=".$id)->find();
		if($user["type"]=="1"){
			db('studentandparent')->where("userid=".$id)->delete();

		}else if($user["type"]=="2"){
			db('teacheranduser')->where("userid=".$id)->delete();
		}

		$where = 'id='.$id;
	
		$list=db('foodstar')->where("userid=".$id)->delete();
		   $list=db('users')->where($where)->delete();
		 
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }

	}
	function order(){
		$request = Request::instance();
		$id =$request->get('id');


		$this->assign('mid',$id);
		return view();
	}
	function getorderlist(){
		$request = Request::instance();
		$mid =$request->get('mid');
		$where="mid=".$mid;
		$count =db('users')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('studentvideo')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			$student = db("student")->where("id=".$value["studentid"])->find();
			$value["studentname"]= $student["truename"];
			$value["videoetime"]= $student["videoetime"];
		}
		
		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);

	}
	function orderdel(){
		$request = Request::instance();
		$id =$request->post('id');

		$where="id=".$id;
		$list=db('studentvideo')->where($where)->delete();
		if($list!==false) {
			return ['status'=>200,'message'=>'操作成功'];
		  }else{
			 return ['status'=>500,'message'=>'操作失败'];
		  }
		
	}
	public function kefu(){
		
		return view();
	}
	public function manage(){
		
		return view();
	}
	function addsuper(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
			
				$data["state"] =$request->post('state');
				$data["truename"] =  $request->post('truename');
				$data["createtime"] =  date("Y-m-d h:i:sa");
				
			if($data["id"]){
				
				if(db('manage')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				$data["username"] =$request->post('username');
				$data["password"] = md5($request->post('password'));
				$data["issuper"] ="1";
				$data["mid"] ="-1";
			  if(db('manage')->where("username='".$data["username"]."'")->find()){
				return ['status'=>500,'message'=>'用户名重复'];

			  }else  if(db('manage')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'添加失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('manage')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
			
			
		
		
	
		
			
		}
		
		
	
	
		return view();
	}
}
